package com.apartmentManage.controller;

import com.apartmentManage.entity.ApartmentTutor;
import com.apartmentManage.service.ApartmentTutorService;
import com.apartmentManage.util.Renter;
import com.apartmentManage.util.StringUntil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/apartmentTutor")
public class ApartmentTutorController {

    @Autowired
     private ApartmentTutorService apartTutorService;

    @GetMapping("/loginView")
    public String LoginView(){
        return "UserLogin";
    }

    @PostMapping("/getByRequirement")
     String getByRequirement(ApartmentTutor apartmentTutor, Model model){
        if(StringUntil.IsBlank(apartmentTutor.getAccountName())
        || StringUntil.IsBlank(apartmentTutor.getPassword()))
            return "UserLogin";
         ApartmentTutor result = apartTutorService.getByRequirement(apartmentTutor);
        new Renter(result==null ? false : true,result);
        model.addAttribute("UserName",apartmentTutor.getAccountName());
        model.addAttribute("apartmentno",apartmentTutor.getApartmentNo());
         return "pages/UserIndex";
    }

    @PostMapping("/updateByOldPassword")
    String updateByOldPassword(ApartmentTutor apartmentTutor){
        if (StringUntil.IsBlank(apartmentTutor.getPassword())
        || StringUntil.IsBlank(apartmentTutor.getOldPassword())
        || apartmentTutor.getOldPassword().equals(apartmentTutor.getPassword()))
            return "redirect:/apartmentTutor/userUpdateByOldPasswordView/"+apartmentTutor.getAccountName();
        boolean flag= apartTutorService.updateByOldPassword(apartmentTutor);
        if (flag)
            return "redirect:/img/On.html";
        return "redirect:/apartmentTutor/userUpdateByOldPasswordView/"+apartmentTutor.getAccountName();
    }

    @GetMapping("userUpdateByOldPasswordView/{userName}")
    public String updateByOldPassword(@PathVariable String userName, Model model){
        model.addAttribute("userName",userName);
        return "userUpdateByOldPassword";
    }
}
